<?php

namespace App\Models;

use Eloquent as Model;

/**
 * Class Organization
 * @package App\Models
 * @version November 14, 2017, 2:07 am UTC
 *
 * @property string name
 * @property in parent_id
 */
class PayFee extends Model
{
    public $table="t_pay_allfee";

    public $connection="ebills";

    public $timestamps=false;

    public $primaryKey='id';

    public $fillable=[];


    /**
     * 车场编号存在约束
     *
     * @param [type] $query
     * @return void
     */
    public function scopeValid($query)
    {
        return $query->where('park_code', '<>', 0);
    }

    /**
     * 支付类型约束
     *
     * @param [type] $query
     * @return void
     */
    public function scopePayType($query, $pay_type = [])
    {
        $pay_type = ! empty($pay_type) ? $pay_type : config('custom.pay_type');
        return $query->whereIn('pay_type', $pay_type);
    }

    /**
     * 当前时间前一个小时内支付记录约束
     *
     * @param [type] $query
     * @param array $time_arr
     * @return void
     */
    public function scopeRangePayTime($query, array $time_arr)
    {
        if($time_arr[0] == '(' && $time_arr[3] == ')'){
            $left  = '>';
            $right = '<';
        }elseif($time_arr[0] == '(' && $time_arr[3] == ']'){
            $left  = '>';
            $right = '<=';
        }elseif($time_arr[0] == '[' && $time_arr[3] == ')'){
            $left  = '>=';
            $right = '<';
        }else{
            $left  = '>=';
            $right = '<=';
        }
        return $query->where('pay_time', $left, $time_arr[1])
                     ->where('pay_time', $right, $time_arr[2]);
    }

    /**
     * 按车场汇总规定时间段内规定收费类型的 总金额
     *
     * @param [type] $times
     * @param [type] $car_type
     * @return void
     */
    public function sumPayGroupByPark($times)
    {
        return $this->valid()
                    ->payType()
                    ->rangePayTime($times)
                    ->selectRaw('park_code, SUM(`pay_amount`) AS pay_amount, SUM(`pay_src_amount`) AS pay_src_amount')
                    ->groupBy('park_code')
                    ->get();
    }


    /**
     * 按车场汇总规定时间内每个车型每种支付方式 支付总额和笔数
     *
     * @param [type] $times
     * @param array $car_type
     * @param array $pay_type
     * @return void
     */
    public function sumCarPayGroupByPark($times, $car_type=[], $pay_type=[])
    {
        $car_type = ! empty($car_type) ? $car_type : config('custom.car_type');
        $pay_type = ! empty($pay_type) ? $pay_type : config('custom.pay_type');

        return collect($car_type)->map(function($v, $k) use ($pay_type, $times){
            $fields = ['park_code'];
            $sql = 'SUM(IF(pay_type = %d, 1, 0)) as %s_%s_count, SUM(IF(pay_type = %d, pay_amount, 0)) as %s_%s_amount';
            foreach($pay_type as $name=>$type){
                $fields[] = sprintf($sql, $type, $k, $name, $type, $k, $name);
            }
            return $this->valid()
                        ->rangePayTime($times)
                        ->selectRaw(implode(',', $fields))
                        ->whereIn('vpl_color', (array)$v)
                        ->groupBy('park_code')
                        ->get()
                        ->toArray();
        })->flatten(1);

    }


    /**
     * 各种支付方式汇总
     *
     * @param [type] $times
     * @param array $pay_type
     * @return void
     */
    public function sumPayTypeGroupByPark($times, $pay_type=[])
    {
        $pay_type = ! empty($pay_type) ? $pay_type : config('custom.pay_type');

        $fields = ['park_code'];
        $sql = 'SUM(IF(pay_type = %d, 1, 0)) as %s_count, SUM(IF(pay_type = %d, pay_amount, 0)) as %s_amount';
        foreach($pay_type as $k=>$v){
            $fields[] = sprintf($sql, $v, $k, $v, $k);
        }

        return $this->valid()
                    ->rangePayTime($times)
                    ->selectRaw(implode(',', $fields))
                    ->groupBy('park_code')
                    ->get();
    }

}
